package com.bnyy.medicalHousekeeper.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.activity.EditIntroductionActivity;
import com.bnyy.medicalHousekeeper.activity.IncomeDetailActivity;
import com.bnyy.medicalHousekeeper.activity.MedicalHousekeeperHomePageActivity;
import com.bnyy.medicalHousekeeper.activity.SettingActivity;
import com.bnyy.medicalHousekeeper.base.App;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.base.RefreshFragment;
import com.bnyy.medicalHousekeeper.bean.MineInfo;
import com.bnyy.medicalHousekeeper.moudle.wallet.activity.WithdrawalActivity;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.kevin.photo_browse.PhotoBrowse;
import com.kevin.photo_browse.ShowType;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/2
 **/
public class MineFragment extends RefreshFragment {
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_setting)
    ImageView ivSetting;
    @BindView(R.id.iv_header)
    ScaleImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_check_qualifications)
    TextView tvCheckQualifications;
    @BindView(R.id.tv_personal_desc)
    TextView tvPersonalDesc;
    @BindView(R.id.tv_withdrawable)
    TextView tvWithdrawable;
    @BindView(R.id.tv_frozen_income)
    TextView tvFrozenIncome;
    @BindView(R.id.tv_accumulated_income)
    TextView tvAccumulatedIncome;
    @BindView(R.id.tv_withdrawal)
    TextView tvWithdrawal;
    @BindView(R.id.tv_serve_count)
    TextView tvServeCount;
    @BindView(R.id.ll_score)
    LinearLayout llScore;
    @BindView(R.id.tv_check_income_detail)
    TextView tvCheckIncomeDetail;

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getMineInfo(refreshLayout);
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ivMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MedicalHousekeeperHomePageActivity.show(mContext, userInfoManager.getLoginUserId());
            }
        });
        ivSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivityImpl.show(mContext, SettingActivity.class);
            }
        });
        tvCheckIncomeDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivityImpl.show(mContext, IncomeDetailActivity.class);
            }
        });
        tvWithdrawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivityImpl.show(mContext, WithdrawalActivity.class);
            }
        });

        ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Intent data = result.getData();
                if (data != null) {
                    String introduction = data.getStringExtra("introduction");
                    tvPersonalDesc.setText(introduction);
                }
            }
        });
        tvPersonalDesc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditIntroductionActivity.show(mContext, tvPersonalDesc.getText().toString(), launcher);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getMineInfo();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getMineInfo();
        }
    }

    private void getMineInfo() {
        getMineInfo(null);
    }

    private void getMineInfo(RefreshLayout refreshLayout) {
        HashMap<String, Object> params = new HashMap<>();
//        params.put("id", userInfoManager.getLoginUserId());
        requestManager.request(requestManager.mJavaRetrofitService.getMineInfo(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<MineInfo>(refreshLayout) {
                    @Override
                    public void onSuccess(MineInfo mineInfo) {
                        super.onSuccess(mineInfo);

                        GlideHelper.setCircleImage(mContext, mineInfo.getUser_image(), ivHeader, 2, Color.WHITE);
                        tvName.setText(mineInfo.getName());
                        tvServeCount.setText("已服务" + mineInfo.getService_person_count() + "人");

                        tvPersonalDesc.setText(mineInfo.getIntroduce());

                        tvWithdrawable.setText("¥" + mineInfo.getWithdrawal_money());
                        SpanUtils.with(tvFrozenIncome)
                                .append("冻结收益").setForegroundColor(ContextCompat.getColor(mContext, R.color.gray_light))
                                .appendLine()
                                .appendLine()
                                .append(mineInfo.getFrozen_money() + "").setBold()
                                .create();

                        SpanUtils.with(tvAccumulatedIncome)
                                .append("累计收益").setForegroundColor(ContextCompat.getColor(mContext, R.color.gray_light))
                                .appendLine()
                                .appendLine()
                                .append(mineInfo.getTotal_money() + "").setBold()
                                .create();

                        int dp5 = SizeUtils.dp2px(5);
                        int starSize = ScreenUtils.getScreenWidth() / 25;

                        int score = mineInfo.getService_score();
                        Object tag = llScore.getTag();
                        boolean b = false;
                        if (tag == null) {
                            b = true;
                        } else {
                            if (((int) tag) != score) {
                                b = true;
                            }
                        }
                        if (b) {
                            llScore.setTag(score);
                            if (llScore.getChildCount() > 0) {
                                llScore.removeAllViews();
                            }
                            if (score <= 0) {
                                llScore.setVisibility(View.INVISIBLE);
                            } else {
                                for (int i = 0; i < score; i++) {
                                    ImageView iv = new ImageView(mContext);
                                    iv.setImageResource(R.mipmap.icon_star_yellow);
                                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(starSize, starSize);
                                    layoutParams.setMargins(dp5, 0, 0, 0);
                                    llScore.addView(iv, layoutParams);
                                }
                            }

                            tvCheckQualifications.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    PhotoBrowse.with(mContext)
                                            .showType(ShowType.MULTIPLE_URL)
                                            .url(mineInfo.getCredentials())
                                            .show();
                                }
                            });
                        }
                    }
                });
    }
}





























